/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nhncorp.neptune.neptune_test.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.nhncorp.neptune.changelog.previous.FileChangeLogServer;
import com.nhncorp.neptune.changelog.previous.FileChangeLogServerIF;
import com.nhncorp.neptune.common.aop.ProxyFactory;
import com.nhncorp.neptune.common.conf.NConfiguration;

public class MiniFileBasedChangeLogCluster extends MiniChangeLogCluster {
  protected Map<String, FileChangeLogServerIF> changeLogServers;
  protected Map<String, Thread> changeLogServerThreads;

  protected NConfiguration conf;

  public MiniFileBasedChangeLogCluster(NConfiguration conf) throws IOException {
    this.conf = conf;
    this.changeLogServers = new HashMap<String, FileChangeLogServerIF>();
    this.changeLogServerThreads = new HashMap<String, Thread>();

    // try {
    // Thread.sleep(5 * 1000);
    // } catch (InterruptedException e1) {
    // return;
    // }
    FileChangeLogServer.doFormat();
    startChangeLogServers();
  }

  protected void startChangeLogServers() throws IOException {
    for (int i = 0; i < 3; i++) {
      NConfiguration changeLogServerConf = new NConfiguration(conf);
      changeLogServerConf.set("neptune.changelog.server.port",
          changeLogServerConf.getInt("neptune.changelog.server.port", 57001)
              + i);
      changeLogServerConf.set("changelog.data.dir", changeLogServerConf
          .get("changelog.data.dir")
          + "/" + (i + 1));

      FileChangeLogServerIF changeLogServer = (FileChangeLogServerIF) ProxyFactory
          .createProxyClass("changeLogServer");
      changeLogServer.init(changeLogServer, changeLogServerConf);

      this.changeLogServers.put(changeLogServer.getHostName(), changeLogServer);
      Thread t = new Thread(changeLogServer, "changeLogServer-" + (i + 1));
      t.start();
      this.changeLogServerThreads.put(changeLogServer.getHostName(), t);
    }
  }

  public void shutdown() throws IOException {
    for (Map.Entry<String, FileChangeLogServerIF> entry : changeLogServers
        .entrySet()) {
      entry.getValue().shutdown();
    }

    for (Thread changeLogServerThread : changeLogServerThreads.values()) {
      changeLogServerThread.interrupt();
    }
  }
}
